module view {
    export class BattleView extends eui.Component {
        closeBtn: eui.Button
        monsterId: number
        skillGroup1: eui.Group
        skillGroup2: eui.Group
        monsterNameLabel: eui.Label
        hpPB1: eui.ProgressBar
        hpPB2: eui.ProgressBar
        supplyLabel1: eui.Label
        supplyLabel2:  eui.Label
        useBtn1: eui.Button
        useBtn2: eui.Button
        supplyIndex: number
        littleBag: eui.Component
        public constructor(monsterId) {
            super()
            this.monsterId = monsterId
            this.addEventListener(eui.UIEvent.COMPLETE,this.onComplete,this)
            this.skinName = "resource/skin/BattleSkin.exml"
        }
      
        private onComplete(): void {
            this.showSupply() 
            this.useBtn1['value'] = 0
            this.useBtn1.addEventListener(egret.TouchEvent.TOUCH_TAP,this.useSupply,this)
            this.useBtn2['value'] = 1
            this.useBtn2.addEventListener(egret.TouchEvent.TOUCH_TAP,this.useSupply,this)
                
            var monster = new game.Monster(totalConfig[this.monsterId])
            game.battleHandle.setBaseInfo(gplayer,monster,this.hpPB1,this.hpPB2,this)
            this.monsterNameLabel.text = monster.name
            this.hpPB1.maximum = gplayer.fightAttri.maxHp
            this.hpPB1.value = gplayer.fightAttri.hp
            this.hpPB2.maximum = monster.fightAttri.maxHp 
            this.hpPB2.value = monster.fightAttri.hp
            
            for(var i in gplayer.fightAttri.skills) {
                var skillId1 = gplayer.fightAttri.skills[i]
                var skillComponent = new SkillComponent(skillId1)
                this.skillGroup1.addChild(skillComponent)
                game.battleHandle.battleInfo.playerInfo1.skillComponents.push(skillComponent)
            }

            for(var i in monster.fightAttri.skills) {
                var skillId2 = monster.fightAttri.skills[i]
                var skillComponent = new SkillComponent(skillId2)
                this.skillGroup2.addChild(skillComponent)
                game.battleHandle.battleInfo.playerInfo2.skillComponents.push(skillComponent)
            }

            var idTimeout: any = egret.setTimeout(function(arg) {
               game.battleHandle.start()
            },this,100)
        }
        
        private showSupply() {
            if(gplayer.supplyBar[0]) {
                var id = gplayer.supplyBar[0].id
                var itemName = totalConfig[id].name
                this.supplyLabel1.text = itemName + 'X' + gplayer.supplyBar[0].stack
            } else {
                this.supplyLabel1.text = ''
            }

            if(gplayer.supplyBar[1]) {
                var id = gplayer.supplyBar[0].id
                var itemName = totalConfig[id].name
                this.supplyLabel2.text = itemName + 'X' + gplayer.supplyBar[1].stack
            } else {
                this.supplyLabel2.text = ''
            }
        }
        
        private useSupply(evt: egret.TouchEvent) {
            var target = evt.target
            var supplyIndex = target.value
            if(gplayer.supplyBar[supplyIndex] && gplayer.fightAttri.hp !== gplayer.fightAttri.maxHp) {
                var item = gplayer.supplyBar[supplyIndex]
                var itemConfig = totalConfig[item.id]
                gplayer.fightAttri.hp += itemConfig.value
                if(gplayer.fightAttri.hp > gplayer.fightAttri.maxHp) {
                    gplayer.fightAttri.hp = gplayer.fightAttri.maxHp
                }
                item.stack -= 1
                if(item.stack === 0) {
                    gplayer.supplyBar[supplyIndex] = null
                }
                this.hpPB1.value = gplayer.fightAttri.hp
                this.showSupply()
            }
        }

        public close(): void {
            var child = this.stage.getChildByName('MapView')
            this.parent.removeChild(this)
            child['adventureTimer'].start() //继续冒险
        }
        
        //打开小背包
        private openLittleBag(evt: egret.TouchEvent): void {
            var target = evt.target
            this.supplyIndex = target.value
            var littleBag = new eui.Component()
            littleBag.skinName = 'resource/control/Bag1Skin.exml'
            this.addChild(littleBag)
            this.littleBag = littleBag
            this.littleBag['bgButton'].addEventListener(egret.TouchEvent.TOUCH_TAP,function() {
                this.parentView.removeChild(this.littleBag)
            },this)
            for(var i in gplayer.bag.items) {
                var item = gplayer.bag.items[i]
                if(!item) {
                    continue
                }
                var itemId = item.id
                var itemConfig = totalConfig[itemId]
                if(itemConfig.propType !== 2) {
                    continue
                }
                var btn: eui.Button = new eui.Button()
                btn.skinName = "resource/control/Button3Skin.exml"
                btn.width = 160
                littleBag['itemsGroup'].addChild(btn)
                var itemId = item.id
                var stack = item.stack
                btn.label = templateName[itemId] + 'X' + stack
                btn['value'] = itemId
                btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.supply,this)
            }
        }
        //添加物品到补给栏
        private supply(evt: egret.TouchEvent) {
            var target = evt.target
            var itemId = target.value
            if(gplayer.supplyBar[this.supplyIndex]) {
                var itemComponent = gplayer.supplyBar[this.supplyIndex]
                gplayer.bag.addItem(itemComponent)
                gplayer.supplyBar[this.supplyIndex] = null
            }

            var itemComponent = gplayer.bag.getItemComponent(itemId)
            gplayer.supplyBar[this.supplyIndex] = itemComponent
            gplayer.bag.removeItemById(itemId)
            this.showSupply()
            this.removeChild(this.littleBag)
        }
    }
    
    class SkillComponent extends eui.Component {
        pBar: eui.ProgressBar
        publicPBar: eui.ProgressBar
        timer: egret.Timer
        skillCd: number
        nameLabel: eui.Label
        completeTime: number
        skillId: number
        public constructor(skillId) {
            super()
            this.skinName = 'resource/skin/SkillControlSkin.exml'
            this.skillId = skillId
            var skillConfig = totalConfig[this.skillId]
            this.nameLabel.text = skillConfig.name
            this.skillCd = skillConfig.cd
            this.publicPBar.thumb['source'] = 'resource/assets/publicCd.png'
        }
        public startSkill() {
            this.pBar.maximum = 100
            this.pBar.slideDuration = 0
            this.pBar.value = 0

            var skillConfig = totalConfig[this.skillId]
            this.pBar.slideDuration = skillConfig.cd
            this.pBar.value = 100
        }

        public startPublic() {
            this.publicPBar.maximum = 100
            this.publicPBar.slideDuration = 0
            this.publicPBar.value = 0
            this.publicPBar.slideDuration = 800
            this.publicPBar.value = 100
        }
        
        public resetPublic(){
            this.publicPBar.slideDuration = 0
            this.publicPBar.value = 0
        }
    }
}
